package com.hjm.project.toolsReward.toolsReward.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.project.system.dict.domain.DictData;
import com.hjm.project.system.dict.service.IDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.toolsReward.toolsReward.domain.ToolsReward;
import com.hjm.project.toolsReward.toolsReward.service.IToolsRewardService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 机具采购奖励标准Controller
 * 
 * @author LOVE
 * @date 2020-11-23
 */
@Controller
@RequestMapping("/toolsReward/toolsReward")
public class ToolsRewardController extends BaseController
{
    private String prefix = "toolsReward/toolsReward";

    @Autowired
    private IToolsRewardService toolsRewardService;

    @Autowired
    private IDictDataService dictDataService;

    @RequiresPermissions("toolsReward:toolsReward:view")
    @GetMapping()
    public String toolsReward()
    {
        return prefix + "/toolsReward";
    }

    /**
     * 查询机具采购奖励标准列表
     */
    @RequiresPermissions("toolsReward:toolsReward:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ToolsReward toolsReward)
    {
        startPage();
        List<ToolsReward> list = toolsRewardService.selectToolsRewardList(toolsReward);
        return getDataTable(list);
    }

    /**
     * 导出机具采购奖励标准列表
     */
    @RequiresPermissions("toolsReward:toolsReward:export")
    @Log(title = "机具采购奖励标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ToolsReward toolsReward)
    {
        List<ToolsReward> list = toolsRewardService.selectToolsRewardList(toolsReward);
        ExcelUtil<ToolsReward> util = new ExcelUtil<ToolsReward>(ToolsReward.class);
        return util.exportExcel(list, "toolsReward");
    }

    /**
     * 新增机具采购奖励标准
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        ToolsReward toolsReward=new ToolsReward();
        List<ToolsReward> list = toolsRewardService.selectToolsRewardList(toolsReward);
        Map<String,Object> map=new HashMap<>(16);
        if (list.size()>0) {
            List<DictData> dictDatas = (List<DictData>)dictDataService.selectDictDataByType("tools_reward_gears");
            for (DictData dictData : dictDatas) {
                for (ToolsReward reward : list) {
                    if (dictData.getDictValue().equals(String.valueOf(reward.getGears()))){
                        map.put("id"+dictData.getDictValue(),reward.getId());
                        map.put("toolsNumber"+dictData.getDictValue(),reward.getToolsNumber());
                        map.put("rewardMoney"+dictData.getDictValue(),reward.getRewardMoney());
                        map.put("gears"+dictData.getDictValue(),reward.getGears());
                    }
                }
            }
            mmap.put("flag","1");
        }else{
            mmap.put("flag","0");
        }
        mmap.put("toolsRewards",map);
        return prefix + "/add";
    }

    /**
     * 新增保存机具采购奖励标准
     */
    @RequiresPermissions("toolsReward:toolsReward:add")
    @Log(title = "机具采购奖励标准", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam Map<String,String> params)
    {
        params.put("addMan",this.getSysUser().getLoginName());
        return toolsRewardService.insertToolsReward(params);
    }

    /**
     * 修改机具采购奖励标准
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ToolsReward toolsReward = toolsRewardService.selectToolsRewardById(id);
        mmap.put("toolsReward", toolsReward);
        return prefix + "/edit";
    }

    /**
     * 修改保存机具采购奖励标准
     */
    @RequiresPermissions("toolsReward:toolsReward:edit")
    @Log(title = "机具采购奖励标准", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ToolsReward toolsReward)
    {
        return toAjax(toolsRewardService.updateToolsReward(toolsReward));
    }

    /**
     * 删除机具采购奖励标准
     */
    @RequiresPermissions("toolsReward:toolsReward:remove")
    @Log(title = "机具采购奖励标准", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(toolsRewardService.deleteToolsRewardByIds(ids));
    }
}
